package persistencia;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Agustin
 */
public class Conexion {

    private String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + System.getProperty("user.dir") + "\\job-society.accdb";
    private Connection conexion;
    private static Conexion instance;

    public Connection getConexion() {
        return conexion;
    }

    private Conexion() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public static Conexion getInstance() {
        if (instance == null) {
            instance = new Conexion();
        }
        return instance;
    }

    public void conectar() throws SQLException {
        try {
            conexion = DriverManager.getConnection(url);

        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public void desconectar() throws SQLException {
        try {
            conexion.close();
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public ResultSet select(String query) throws SQLException {
        try {
            Statement st = conexion.createStatement();
            ResultSet rs = st.executeQuery(query);
            return rs;
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public void insert(String query) throws SQLException {
        try {
            Statement st = conexion.createStatement();
            st.execute(query);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }
}
